import { Component, OnInit, Input } from '@angular/core'
import { Widget } from '../../model'
import { JsonApiService } from 'app/core/api/json-api.service'

declare var layui: any

@Component({
  selector: 'install-factory-details',
  templateUrl: './install-factory-details.html',
  styleUrls: ['./install-factory-details.css']
})
export class InstallFactoryDetailsComponent implements OnInit, Widget {
  @Input() componentData: any
  companyIds: any = '' // 选中公司
  startTime: any = '' // 开始时间
  endTime: any = '' // 结束时间
  tableData: any = [] //列表数据
  treeTable: any = null
  isShowModal: boolean = false //常用项目弹窗
  loading: boolean = false
  constructor(private jsonApiService: JsonApiService) { }
  initLayui() {
    const that = this
    layui.use(function () {
      that.treeTable = layui.treeTable
      that.treeTable.render({
        elem: '#install-factory-details-table',
        data: that.tableData,
        height: '445px',
        tree: {
          view: {
            showIcon: false,
            dblClickExpand: false
          }
        },
        cols: [
          [
            { field: 'name', title: '厂区名称', fixed: 'left', minWidth: 150 },
            { field: 'installNum', title: '安装量', minWidth: 90 },
            { field: 'repairedNum', title: '维修量', minWidth: 90 },
            { field: 'installWindowPeriod', title: '空窗期(安装)<i class="fa fa-question-circle" title="厂区最后一次安装设备距今的时间(单位:天)" style="color:#555555;font-size: 16px;cursor: pointer;margin-left:5px;"></i>', minWidth: 140 },
            { field: 'repairedWindowPeriod', title: '空窗期(维修)<i class="fa fa-question-circle" title="厂区最后一次维修设备距今的时间(单位:天)" style="color:#555555;font-size: 16px;cursor: pointer;margin-left:5px;"></i>', minWidth: 140 }
          ]
        ],
        page: false
      })
      // 树形表格单击展开
      that.treeTable.on('row(install-factory-details-table)', data => {
        const expandFlag = !data.data.LAY_EXPAND
        const index = data.index
        that.treeTable.expandNode('install-factory-details-table', {
          index,
          expandFlag
        })
      })
      let laydate = layui.laydate;
      let layer = layui.layer
      let layTime = laydate.render({
        elem: "#install-factory-details-date",
        btns: ['confirm'],
        range: true,
        shortcuts: [
          {
            text: "上周",
            value: function () {
              let value = [];
              let date1 = new Date();
              let n = date1.getDay() == 0 ? 7 + 6 : 7 + (date1.getDay() - 1)
              date1.setDate(date1.getDate() - n);
              value.push(date1);
              let date2 = new Date();
              let n2 = date2.getDay() == 0 ? 1 + 6 : 1 + (date2.getDay() - 1)
              date2.setDate(date2.getDate() - n2);
              value.push(date2);
              return value;
            }()
          },
          {
            text: "昨天",
            value: function () {
              let value = [];
              let date1 = new Date();
              date1.setDate(date1.getDate() - 1);
              value.push(date1);
              value.push(date1);
              return value;
            }()
          },
          {
            text: "上个月",
            value: function () {
              var value = [];
              var date1 = new Date();
              date1.setMonth(date1.getMonth() - 1);
              date1.setDate(1);
              date1.setHours(0, 0, 0, 0);
              value.push(date1);
              var date2 = new Date();
              date2.setDate(1);
              date2.setHours(0, 0, 0, 0);
              let date3 = date2.getTime() - 1;
              value.push(new Date(date3));
              return value;
            }()
          },
          {
            text: "本月",
            value: function () {
              let value = [];
              let date = new Date();
              let year = date.getFullYear()
              let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
              let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
              let date1 = year + '-' + month + '-01'
              value.push(date1);
              let date2 = year + '-' + month + '-' + day
              value.push(date2);
              return value;
            }()
          }
        ],
        value: that.getRencentDay(),
        max: common.getNowTime(),
        isInitValue: true,
        done: function (value, date, endDate) {
          // console.log(value, 'value'); // 日期字符，如： 2017-08-18
          // console.log(date, 'date'); // 包含年月日时分秒各项值的对象
          // console.log(endDate, 'endDate'); // 结束日期时间对象，当设置 range 时才会返回。对象成员同上。
          const dateArray = value.split(' - ')
          that.startTime = dateArray[0]
          that.endTime = dateArray[1]

          const start = new Date(that.startTime + ' ' + date.hours + ':' + date.minutes + ':' + date.seconds)
          const end = new Date(that.endTime + ' ' + endDate.hours + ':' + endDate.minutes + ':' + endDate.seconds)
          const dayDate = (end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24)
          if (dayDate > 90) {
            layer.msg('日期选择不能超过90天!');
            // layTime.hint('日期选择不能超过90天!')
          } else {
            that.requestData()
          }
        }
      })
    })
  }
  ngOnInit() {
    this.companyIds = this.componentData.componentsConfig
    this.initLayui()
  }
  requestData() {
    const that = this
    let postParam = {
      companyIds: this.companyIds === 'default' ? '' : this.companyIds,
      startTime: this.startTime ? this.startTime + ' 00:00:00' : '',
      endTime: this.endTime ? this.endTime + ' 23:59:59' : ''
    }
    this.loading = true
    this.jsonApiService.http_obj('control/station/install/company/installCompanyDetail.html', postParam, false).subscribe(data => {
      this.tableData = []
      this.loading = false
      if (data.success) {
        this.tableData = data.data || []
        that.treeTable.reloadData('install-factory-details-table', {
          data: that.tableData
        })
      }
    })
  }
  //默认前一周
  getRencentDay() {
    let date1 = new Date();
    let n = date1.getDay() == 0 ? 7 + 6 : 7 + (date1.getDay() - 1)
    date1.setDate(date1.getDate() - n);
    let value1 = common.getTimeDay(date1)
    this.startTime = value1
    let date2 = new Date();
    let n2 = date2.getDay() == 0 ? 1 + 6 : 1 + (date2.getDay() - 1)
    date2.setDate(date2.getDate() - n2);
    let value2 = common.getTimeDay(date2)
    this.endTime = value2
    this.requestData() //渲染表格数据
    return value1 + ' - ' + value2;
  }

  // 在组件销毁之前调用
  ngOnDestroy() { }
  //打开弹窗
  openClick() {
    this.isShowModal = true
  }
  //关闭弹窗
  closeDialog(data) {
    // console.log(data, 'data')
    this.isShowModal = data.isShowModal
    const refresh = data.isRefresh
    if (refresh) {
      this.companyIds = data.companyIds
      this.initLayui()
    }
  }
}
